package com.zwl.openandclose;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * ┃　　　┃   神兽保佑
 * ┃　　　┃   代码无BUG！
 * ┃　　　┗━━━┓
 * ┃　　　　　　　┣┓
 * ┃　　　　　　　┏┛
 * ┗┓┓┏━┳┓┏┛
 * ┃┫┫　┃┫┫
 * ┗┻┛　┗┻┛
 * create by 2017/3/28 14:56
 * annotation:
 */
public class ExpandActivity extends Activity {

    private Context c;
    private List<String> parent;
    private ExpandListAdapter adapter;
    private ExpandableListView expand;

    boolean isOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expand);

        c = this;

        init();

        initData();
    }

    private void initData() {
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                open();
//                openOrCloseBtn();
            }
        });

        final Button close = (Button) findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                close();
            }

        });
        final Button openORclose = (Button) findViewById(R.id.openORclose);
        openORclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openOrClose();
            }

        });
    }

    //点击展开
    private void open() {

        int count = expand.getCount();
        for (int i = 0; i < count; i++) {

            expand.expandGroup(i);

        }
    }

    //点击闭合
    private void close() {
        int count = expand.getCount();
        for (int i = 0; i < count; i++) {
            expand.collapseGroup(i);

        }

    }

    private void openOrClose() {

        if (isOpen){
            close();
            isOpen = true;
        }else {
            open();
            isOpen = false;
        }
    }


    private void init() {

        List<String> parent = new ArrayList<>();
        List<String> child = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            parent.add("-----" + i + "-----");

            for (int j = 0; j < i; j++) {
                child.add("~~~~" + j + "~~~~");

            }
        }


        Map<List<String>, List<String>> dataGroup = new HashMap<>();

        adapter = new ExpandListAdapter(c, parent, child, dataGroup);

        expand = (ExpandableListView) findViewById(R.id.expand);

        expand.setAdapter(adapter);


    }
}
